package cc.minos.question
{
	import cc.minos.console.Console;
	import cc.minos.utils.FilterUtil;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.filters.ColorMatrixFilter;
	import flash.utils.getDefinitionByName;
	import cc.minos.question.plugins.*;
	
	/**
	 * ...
	 * 教育题目类
	 * 支持单选，多选，连线(1对1，多对1)，填空(有序，无序)，判断，拖动(多对1), 拖动(组句子), 播放声音
	 * 一道题可由各类型题目组成
	 * @author Minos
	 */
	public class Question extends Sprite
	{
		//灰色滤镜
		public static const grayFilter:ColorMatrixFilter = FilterUtil.GRAY_FILTER;
		//题目路径
		public static var QUESTION_PATH:String = "";
		//开启插件列表
		public static var activatedPlugins:Object = {};
		public static const acronyms:Object = { 
				'choice':	'ChoicePlugin' ,
				'drag'	:	'DragPlugin' ,
				'line'	:	'DrawLinePlugin' ,
				'fillin':	'FillInPlugin' ,
				'judgment':	'JudgmentPlugin' ,
				'multi'	:	'MultiChoicePlugin' ,
				'order'	:	'OrderFillInPlugin' ,
				'sentence':	'SentencePlugin' ,
				'single':	'SingleChoicePlugin' ,
				'sound'	:	'SoundPlugin' ,
				'view'	:	'ViewPlugin'
			}
		
		//数据
		private var data:Object;
		//容器
		private var container:DisplayObjectContainer;
		//资源
		private var _skin:Sprite;
		//插件列表
		private var plugins:Array;
		//题目实例名
		private var questionName:String = "q_$_mc";
		//答案剪辑
		private var __answerMc:MovieClip;
		//是否已经检查
		private var _isCheck:Boolean = false;
		
		public function Question()
		{
			//构造
		}
		
		/**
		 * 初始化
		 */
		protected function init():void
		{
			//组件数组
			plugins = [];
			//题目初始坐标
			skin.x = skin.y = 2;
			//题目组件数据
			var _ary:Array = data[ "data" ];
			//
			//循环根据组件创建代码
			for ( var i:int = 0 , len:int = _ary.length ; i < len ; i++ )
			{
				var d:Object = _ary[ i ];
				var _class:Class;
				
				if (  d.type in activatedPlugins )
				{
					_class = activatedPlugins[ d.type ];
				}else if ( acronyms[ d.type ] in activatedPlugins  ) {
					_class = activatedPlugins[ acronyms[ d.type ] ];
				}else {
					//插件未开启
					Console.log( "Not Activated Plugin > " + "id: " + id + "type: " + d.type , this , 4 );
					return;
				}
				
				//获取插件
				var plugin:QuestionPlugin = new _class();
				//获取资源
				var _skin:DisplayObjectContainer = null;
				var _name:String = d.name ? d.name : d.localName ? d.localName : "";
				if ( skin.getChildByName( _name ) != null )
				{
					_skin = skin.getChildByName( _name ) as DisplayObjectContainer;
				}
				else if ( skin.getChildByName( questionName.replace( "$" , _name ) ) != null )
				{
					_skin = skin.getChildByName( questionName.replace( "$" , _name ) ) as DisplayObjectContainer;
				}
				else
				{
					_skin = skin;
				}
				//绑定数组和资源到插件
				plugin.bind( _skin , d );
				plugins[ i ] = plugin;
			}
			//获取答案剪辑
			__answerMc = skin.getChildByName( "answer_mc" ) as MovieClip;
		}
		
		/**
		 * 绑定
		 * @param	skin			:	题目
		 * @param	container		:	容器
		 * @param	data			:	数据
		 */
		public function bind( skin:Sprite , container:DisplayObjectContainer , data:Object , isInit:Boolean = true ):void
		{
			_skin = skin;
			this.container = container;
			this.data = data;
			if ( isInit )
				init();
		}
		
		/**
		 * 启用
		 * 添加题目到容器并启用题目的组件
		 */
		public function activate():void
		{
			if ( skin is DisplayObject )
				container.addChild( skin );
			if ( __answerMc && __answerMc.currentFrame == 2 )
				return;
			for each ( var i:QuestionPlugin in plugins )
			{
				i.activate()
			}
		}
		
		/**
		 * 取消
		 * 移除题目并取消组件
		 */
		public function cancel():void
		{
			if ( container.contains( skin ) )
				container.removeChild( skin );
			for each ( var i:QuestionPlugin in plugins )
			{
				i.cancel();
			}
		}
		
		/**
		 * 屏蔽
		 * 取消组件显示答案
		 */
		public function screen():Boolean
		{
			if ( __answerMc && __answerMc.currentFrame == 1 )
			{
				for each ( var i:QuestionPlugin in plugins )
				{
					i.cancel();
				}
				__answerMc.gotoAndStop( 2 );
				_isCheck = true;
				return false;
			}
			return true;
		}
		
		/**
		 * 重置题目
		 */
		public function reset():void
		{
			for each ( var i:QuestionPlugin in plugins )
			{
				i.reset();
			}
			_isCheck = false;
			if ( __answerMc )
				__answerMc.gotoAndStop( 1 );
		}
		
		/**
		 * 检查题目
		 * @return
		 */
		public function check():Boolean
		{
			var b:Boolean = true;
			for each ( var i:QuestionPlugin in plugins )
			{
				if ( !i.check() )
					b = false;
			}
			return b;
		}
		
		/**
		 * 获取题目分数
		 * @return
		 */
		public function get score():Number
		{
			var _socre:Number = 0;
			for each ( var i:QuestionPlugin in plugins )
			{
				_socre += i.score;
			}
			
			return _socre;
		}
		
		public function get totalScore():Number
		{
			return data[ "score" ] !=undefined ? Number( data[ "score" ] ) : 0;
		}
		
		public function get isCheck():Boolean
		{
			return _isCheck;
		}
		
		public function get skin():Sprite
		{
			return _skin;
		}
		
		public function get point():String
		{
			return data[ "point" ] !=undefined ? String( data[ "point" ] ) : data[ "checkPoint" ] !=undefined ? String( data[ "checkPoint" ] ) : "";
		}
		
		public function get id():int
		{
			return data[ "id" ] !=undefined ? int( data[ "id" ] ) : 0;
		}
	
	}

}